//
//  UpdateUserBirthDateController.swift
//  d11
//
//  Created by rhonin on 2018/10/30.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
import SwiftyJSON
class UpdateUserBirthDateController: UpdateRootViewController,PGDatePickerDelegate {
    // 距离父视图顶部间距
    @IBOutlet weak var bottomViewTopConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var buttonSingnin: UIButton!

    @IBOutlet weak var viewCome: UIView!

    @IBOutlet weak var textbirth: UITextField!
  
    override func viewDidLoad() {
        super.viewDidLoad()
        self.viewCome.layer.borderColor = RGBAlpa(219,219,219,1).cgColor
        // Do any additional setup after loading the view.
        let layerGradient = CAGradientLayer()
        layerGradient.colors = [
            UIColor.init(red: 1, green: 0.78, blue: 0.45, alpha: 1).cgColor,
            UIColor.init(red: 0.99, green: 0.57, blue: 0.33, alpha: 1).cgColor
        ]
        layerGradient.frame = CGRect(x: 0, y: 0, width: buttonSingnin.frame.width, height: buttonSingnin.frame.height+50)
        buttonSingnin.layer.addSublayer(layerGradient)
        buttonSingnin.layer.cornerRadius = 5
        self.title = "更改出生日期"
        self.bottomViewTopConstraint.constant = kNavigationBarHeight
        // 设置导航栏默认返回按钮
        self.leftButtonActionWithImg("icon_default_back")
    }

    @IBAction func clickBirthday(_ sender: UIButton) {
        
        let manager = PGDatePickManager()
        let picker = manager.datePicker
        picker?.delegate = self
        picker?.datePickerMode = .date
        self.present(manager, animated: false, completion: nil)
    }
    
    func datePicker(_ datePicker: PGDatePicker!, didSelectDate dateComponents: DateComponents!) {
        let year = "\(dateComponents.year!)"
        let month = dateComponents.month! >= 10 ? "\(dateComponents.month!)" : "0\(dateComponents.month!)"
        let day = dateComponents.day! >= 10 ? "\(dateComponents.day!)" : "0\(dateComponents.day!)"
        textbirth.text = year + "-" + month + "-" + day
        
        //设置时区  不会相差八小时
        
    
    }
    
    @IBAction func updateUserDate(){
        if textbirth.text!.count == 0 {
            UIHelper.alertSimpleMessage(about: "请输入生日", withDescription: "", in: self)
            return
        }
        let progress = MBProgressHUD.showMessageNoHiden("", to: self.view)
        weak var this = self;
        RequestHelper.post(atPath: "/clientsAPIs/SaveDOB", withData: ["birth":textbirth.text!], withCompletion: { data in
            let json = JSON(data)
            let code = json["success"].boolValue
            if code {
                DispatchQueue.main.async {
                    progress.removeFromSuperview()
                    UIHelper.alertSimpleMessage(about: "更改日期成功", withDescription: "", in: self)
                    this?.perform(#selector(this?.popcont), with: nil, afterDelay: 0.5)
                }
            }else{
                DispatchQueue.main.async {
                     progress.removeFromSuperview()
                    UIHelper.alertSimpleMessage(about: "更改失败", withDescription: json["msg"].stringValue, in: self)
                    this?.perform(#selector(this?.popcont), with: nil, afterDelay: 0.5)
                    
                }
            }
        },withError: {_ in
             UIHelper.alertSimpleMessage(about: "服务器异常，\(getErrorString())", withDescription: "", in: self)
        })
    }
    @objc func popcont()  {
        self.navigationController?.popViewController(animated: true)
    }

}
